package com.adingxiong.quickstart.project.controller;


import com.adingxiong.quickstart.project.entity.Person;
import com.adingxiong.quickstart.project.service.IPersonService;
import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/person")
@Api(tags = "用户模块")
public class PersonController {

    @Autowired
    private IPersonService personService;


    @PostMapping("/save")
    @ApiOperation(value = "新增用户信息")
    public Result saveInfo( Person person){
        if(person == null){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        boolean insert = personService.saveOrUpdate(person);
        return new Result(insert ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),insert ? "新增成功" : "新增失败",insert);

    }

    @PostMapping("/update")
    @ApiOperation(value = "根据id修改信息")
    public Result updateInfo(Person  person){
        if(person == null){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        boolean update = personService.updateById(person);
        return new Result(update ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),update ? "修改成功" : "修改失败",update);
    }


    @ApiOperation(value = "条件查询")
    @GetMapping(value = "/list")
    public Result<Page<Person>> getList(
            @ApiParam(value = "姓名") @RequestParam(value = "name",required = false) String name,
            @ApiParam(value = "电话") @RequestParam(value = "phone", required = false) String phone,
            @ApiParam(value = "身份证") @RequestParam(value = "idnum", required = false) String idnum,
            @ApiParam(value = "关键字") @RequestParam(value = "keywords", required = false) String keywords,
            @ApiParam(value = "当前页") @RequestParam(value = "currentPage" ,required = false,defaultValue = "0") Integer currentPage,
            @ApiParam(value = "页数") @RequestParam(value = "pageSize" ,required = false, defaultValue = "10") Integer pageSize
            ){
        QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.eq("name",name);
        }
        if(!StringUtils.isEmpty(phone)){
            queryWrapper.eq("phone",phone);
        }
        if(!StringUtils.isEmpty(idnum)){
            queryWrapper.eq("idnum",idnum);
        }
        if(!StringUtils.isEmpty(keywords)){
            queryWrapper.like("name",keywords).like("idnum",keywords).like("phone",phone);
        }
        Page page = new Page();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        Page<Person> res = personService.page(page,queryWrapper);
        return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),page);

    }


    @ApiOperation(value = "根据id删除用户")
    @GetMapping("/del")
    public Result delInfo(
            @ApiParam(value = "id",required = true) @RequestParam(value = "id")Integer id
    ){
        if(StringUtils.isEmpty(id)){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        boolean del = personService.removeById(id);
        return new Result(del ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),del ? "删除成功" : "删除失败",del);
    }

    @ApiOperation(value = "表单唯一性检验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type" , value = "检验的类型(name,phone,email,idnum)" , dataType ="String" ,paramType = "query" ),
            @ApiImplicitParam(name = "value" , value = "检验的值" , dataType ="String" ,paramType = "query" )
    })
    @RequestMapping(value = "/checkOnly" ,method =  RequestMethod.GET)
    public Result onlyCheck (String type , String value){
        if(StringUtils.isEmpty(type) || StringUtils.isEmpty(value)){
            return  new Result().setCode(RetCode.PARAM_ERROR.getCode()).setMsg("参数为空");
        }
        if(!type.equals("name") && !type.equals("phone") && !type.equals("email") && !type.equals("idnum")){
            return  new Result().setCode(RetCode.PARAM_VALUE_ERROR.getCode()).setMsg(RetCode.PARAM_VALUE_ERROR.getMsg()).setData(false);
        }
        String tip = null;
        QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
        switch (type) {
            case "name":
                queryWrapper.eq("name", value);
                tip = "用户名已存在";
                break;
            case "phone":
                queryWrapper.eq("phone", value);
                tip = "手机号码已存在";
                break;
            case "email":
                queryWrapper.eq("email",value);
                break;
            case "idnum":
                queryWrapper.eq("idnum" ,value);
                break;
            default:
        }
        Person user = personService.getOne(queryWrapper,false);
        if(user ==null) {
            return new Result <>().setCode(RetCode.SUCCESS).setData(true).setMsg("校验通过");
        }
        return new Result <>().setCode(RetCode.SUCCESS).setData(false).setMsg(tip);
    }

}

